﻿Imports DNP.GestionComercial.BE
Imports DNP.GestionComercial.Util
Public Class sisFunciones

#Region "VARIABLES"
    'Private msTextoDdl As String
    'Private msValorDdl As String
    Private msValorFiltro As String
    Private msCampoFiltro As String
  
#End Region
    Shared Sub Carga_ComboFromEnum(ByVal cboData As ComboBox, ByVal tipoDatoEnumerado As System.Type, _
                               Optional ByVal seleccionarPrimerItem As Boolean = False, _
                                Optional ByVal valorInicial As String = "-1",
                                  Optional ByVal textoInicial As String = "[Seleccionar]")

        Dim arrValores As Array = System.Enum.GetValues(tipoDatoEnumerado)
        Dim lisValores As New List(Of PARAMETROBE)

        For Each dato As Object In arrValores
            Dim parParametro As New PARAMETROBE
            parParametro.PARAMETRO01 = dato.GetHashCode.ToString
            parParametro.PARAMETRO02 = dato.ToString.Replace("_", " ")
            lisValores.Add(parParametro)
        Next

        Load_Combo(cboData, lisValores, "PARAMETRO01", "PARAMETRO02", seleccionarPrimerItem, valorInicial)
    End Sub
    ''' <summary>
    ''' carga información en combobox
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <param name="cbodata">control combobox</param>
    ''' <param name="data">list con la informacion</param>
    ''' <param name="campoCodigo">campo codigo</param>
    ''' <param name="campoNombre">campo nombre</param>
    ''' <param name="textoInicial">texto inicial</param>
    ''' <remarks></remarks>
    Shared Sub Load_Combo(Of T As New)(ByRef cbodata As ComboBox, _
                                  ByVal data As List(Of T), _
                                  ByVal campoCodigo As String, _
                                  ByVal campoNombre As String, _
                                  Optional ByVal seleccionarPrimerItem As Boolean = False, _
                                  Optional ByVal valorInicial As String = "-1",
                                  Optional ByVal textoInicial As String = "[Seleccionar]")

        ' If valorInicial <> "-1" Then
        Dim objItem As New T

        SetValue_Property(objItem, campoCodigo, valorInicial)
        SetValue_Property(objItem, campoNombre, textoInicial)

        data.Insert(0, objItem)
        'End If

        cbodata.DataSource = data
        cbodata.DisplayMember = campoNombre
        cbodata.ValueMember = campoCodigo

        If seleccionarPrimerItem Then
            cbodata.SelectedIndex = 0
        Else
            cbodata.SelectedValue = DBNull.Value
        End If

    End Sub
    ''' <summary>
    ''' Asigna un valor a la propiedad de un objeto
    ''' </summary>
    ''' <typeparam name="T"></typeparam>
    ''' <param name="objeto">objeto</param>
    ''' <param name="propiedad">propiedad al que se quiere asignar un valor</param>
    ''' <param name="valor">valor asignado</param>
    ''' <remarks></remarks>
    Shared Sub SetValue_Property(Of T As New)(ByVal objeto As T, ByVal propiedad As String, ByVal valor As Object)
        Try
            objeto.GetType().GetProperty(propiedad.ToUpper).SetValue(objeto, valor, Nothing)
        Catch ex As Exception
            objeto.GetType().GetProperty(propiedad.ToUpper).SetValue(objeto, CInt(valor), Nothing)
        End Try

    End Sub
    Public Sub EnlazarGrilla(Of T)(ByRef pGrilla As DataGridView, ByVal plstFuente As List(Of T))

        LimpiarGrilla(pGrilla)

        pGrilla.DataSource = plstFuente
        pGrilla.Refresh()


    End Sub


    Public Sub EnlazarGrillaFiltro(Of T)(ByRef pGrilla As DataGridView, ByVal plstFuente As List(Of T), ByVal pValor As String, ByVal pColumna As String)

        LimpiarGrilla(pGrilla)
        pGrilla.DataSource = Filtrar(plstFuente, pValor, pColumna)
        pGrilla.Refresh()


    End Sub
    
    Public Sub LimpiarGrilla(ByRef pGrilla As DataGridView)
        pGrilla.DataSource = Nothing

    End Sub
    Public Sub LimpiarListBox(ByRef pListBox As ListBox)
        pListBox.DataSource = Nothing
    End Sub
    Public Sub LimpiarDropDownList(ByRef pDropDownList As ComboBox)
        pDropDownList.DataSource = Nothing
    End Sub

    Public Function ObjetoEnLista(Of T)(ByVal psPropComparacion As String, ByVal poObjetoComparar As T, ByVal plstFuente As List(Of T)) As Boolean
        msCampoFiltro = psPropComparacion
        msValorFiltro = LeerPropiedadObjeto(poObjetoComparar, msCampoFiltro, "0").ToString()

        Return plstFuente.Exists(AddressOf PredicadoFiltrar)

    End Function


    Public Function IndiceObjetoEnLista(Of T)(ByVal psPropComparacion As String, ByVal poObjetoComparar As T, ByVal plstFuente As List(Of T)) As Integer
        msCampoFiltro = psPropComparacion
        msValorFiltro = LeerPropiedadObjeto(poObjetoComparar, msCampoFiltro, "0").ToString()

        Return plstFuente.FindIndex(AddressOf PredicadoFiltrar)

    End Function

    ''' Funcion de filtrado en lista de objetos de negocio

    Public Function Filtrar(Of T)(ByVal ptListaFuente As List(Of T), _
                                            ByVal psValorFiltro As String, _
                                            ByVal psCampoFiltro As String _
                                            ) As List(Of T)

        Dim lstResultado As List(Of T)

        msCampoFiltro = psCampoFiltro
        msValorFiltro = psValorFiltro

        lstResultado = ptListaFuente.FindAll(AddressOf PredicadoFiltrar)

        Return lstResultado

    End Function

    Public Function QUITAR(Of T)(ByVal ptListaFuente As List(Of T), _
                                           ByVal psValorFiltro As String, _
                                           ByVal psCampoFiltro As String _
                                           ) As List(Of T)

        '     Dim lstResultado As List(Of T)

        msCampoFiltro = psCampoFiltro
        msValorFiltro = psValorFiltro



        ptListaFuente.RemoveAll(AddressOf PredicadoFiltrar)

        Return ptListaFuente

    End Function

    Public Function QUITAR_DE(Of T)(ByRef ptListaFuente As List(Of T), _
                                         ByVal psValorFiltro As String, _
                                         ByVal psCampoFiltro As String _
                                         ) As List(Of T)

        '     Dim lstResultado As List(Of T)

        msCampoFiltro = psCampoFiltro
        msValorFiltro = psValorFiltro



        ptListaFuente.RemoveAll(AddressOf PredicadoFiltrar)

        Return ptListaFuente

    End Function


    Public Function PredicadoFiltrar(Of T)(ByVal pItem As T) As Boolean
        Return LeerPropiedadObjeto(pItem, msCampoFiltro, "-1").ToString.ToLower.Contains(msValorFiltro.ToLower)

    End Function
    Public Function PredicadoOrdenar(Of T)(ByVal pItem As T) As Boolean
        Return LeerPropiedadObjeto(pItem, msCampoFiltro, "-1").ToString.ToLower.Contains(msValorFiltro.ToLower)

    End Function
    Public Shared Function LeerPropiedadObjeto(ByVal pObjeto As Object, _
                                              ByVal psNombrePropiedad As String, _
                                              ByVal psValorDefecto As Object) As Object

        Dim piInfoPropiedad As System.Reflection.PropertyInfo


        piInfoPropiedad = pObjeto.GetType().GetProperty(psNombrePropiedad)

        If Not piInfoPropiedad Is Nothing Then
            Return piInfoPropiedad.GetValue(pObjeto, Nothing)
        End If

        Return psValorDefecto
    End Function
End Class


